
// JavaScript Document
em = {};

//获取URL参数
em.UrlGet=function() {
    var args = {};
    var query = location.search.substring(1);//取地址栏中问号后第一个参数
    var pairs = query.split("&");
    for (var i = 0; i < pairs.length; i++) {
        var pos = pairs[i].indexOf('=');
        if (pos == -1) continue;
        var argname = pairs[i].substring(0, pos);
        var value = pairs[i].substring(pos + 1);
        value = decodeURIComponent(value);
        args[argname] = value;
    }
    
    return args;
};

em.is_weixin=function(){
    var ua = navigator.userAgent.toLowerCase();
    if(ua.match(/MicroMessenger/i)=="micromessenger") {
        return true;
    } else {
        return false;
    }
};

function getUserInfo(url,code){
	var ctx=window.daijia.ctx;
	$.post(url,{'code':code},function(data){
		if(data.object){
			$("#wxuser").val(data.object.openId);
			$("#telephone").val(data.object.phone);
		}
		
	},'json');
	
};

function URLEncode (clearString) {
	var output = '';
		var x = 0;
	clearString = clearString.toString();
	var regex = /(^[a-zA-Z0-9-_.]*)/;
	while (x < clearString.length) {
 		var match = regex.exec(clearString.substr(x));
 		if (match != null && match.length > 1 && match[1] != '') {
     		output += match[1];
   			x += match[1].length;
 		}else{
   			if (clearString.substr(x, 1) == ' ') {
     		//原文在此用 clearString[x] == ' ' 做判断, 但ie不支持把字符串当作数组来访问, 
    		//修改后两种浏览器都可兼容 
     		output += '+';
   			}
			else{
    		var charCode = clearString.charCodeAt(x);
     		var hexVal = charCode.toString(16);
    		output += '%' + ( hexVal.length < 2 ? '0' : '' ) + hexVal.toUpperCase();
  			}
			x++;
 		}
	}
	 return output;
};

function calculagraph(times){
	var _btn= $("#yzNumber");
	_btn.attr('disabled','disabled');
						
	var time = 120-times;
	_btn.text(time+"s");
						
	var	L = setInterval(function(){
	time--;
	_btn.text(time+"s");
	if(time <= 0){
		_btn.removeAttr('disabled');
		_btn.text('获取验证码');
		clearInterval(L);
	}
	},1000);
}

function locationMe(){
	
	if(navigator && navigator.geolocation){
		navigator.geolocation.getCurrentPosition(function (position) {
		    var coords = position.coords;
		    
		    window._coords = coords;
		    
		    $("#lat").val(coords.latitude);  
			$("#lng").val(coords.longitude); 
		    translateCoords(coords);
		    
		},function(error){
		    switch(error.code) {
		        case error.TIMEOUT:
		        	$.scojs_message("定位超时，请刷新重试。", $.scojs_message.TYPE_ERROR);
		        	$("#place").attr('placeholder','定位失败，请输入地址');
		          	break;
		        case error.POSITION_UNAVAILABLE:
		        	$.scojs_message("未能获取到位置信息，请更换浏览器。", $.scojs_message.TYPE_ERROR);
		            break;
		        case error.PERMISSION_DENIED:
		        	$.scojs_message("定位失败，请先允许定位。", $.scojs_message.TYPE_ERROR);
		            break;
		        case error.UNKNOWN_ERROR:
		        	$.scojs_message("定位失败，请重新刷新页面重试。", $.scojs_message.TYPE_ERROR);
		            break;
		    }
		    $('#loadblacklayer').hide();
		},{enableHighAccuracy:true,timeout:5000,maximumAge:30000});
		
	}else{
		$.scojs_message("您的浏览器不能定位，请更换浏览器。", $.scojs_message.TYPE_ERROR);
	}
}

function translateCoords(coords){
	
	var loc = coords.latitude+','+coords.longitude;
	
	var param = {ak:'0Bf0928e769210fd88e064cd0cdf84d8',location:loc,output:'json',pois:0,coordtype:'wgs84ll'};
	    
	$.ajax({
		url:"https://api.map.baidu.com/geocoder/v2/",
		type:"GET",
		async:false,
		dataType:'jsonp',
		jsonp:'callback',
		data:param,
		success:function(resp){
			if(null!=resp && ""!=resp && resp.status == 0){
				city=resp.result.addressComponent.city;
				var addressComponent = resp.result.addressComponent;
				var address =addressComponent.district + addressComponent.street +	addressComponent.street_number;				
				$("#place").val(address);
				$('#loadblacklayer').hide();
			}
		},
	});
}

$(document).ready(function(){
	
	var ctx=window.daijia.ctx;
	
	var backlayer ="<div id='loadblacklayer'><div style='text-align: center;margin-left: auto;margin-right: auto;width: 140px; border-radius:4px;height: 140px; background-color:#000; color: #FFF;'><img id='picture' src='"+ctx+"/assets/img/q.gif'  width='100px'/><p id='writing'>请稍等...</p></div></div>";
	$("body").append(backlayer);
	var height =  $("body").height()/2 - 70;
	$('#loadblacklayer').find("div").css({"margin-top":height});
	$('#loadblacklayer').css({"z-index":"9","background":"#fff","opacity":"0.5","position":'absolute',"top":0,"width":"100%","height":"100%"});
	$(window).resize(function(){
		var height =  $("body").height()/2 - 70;
		$('#loadblacklayer').find("div").css({"margin-top":height});
	});
	$('#loadblacklayer').hide();
	
});
